//
//  LWWelcomeVC.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/22.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit
import SDWebImage


//ad_background
//avatar_default_big
//欢迎页面控制器
class LWWelcomeVC: UIViewController {

    //背景图片
    private lazy var bgImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    
    //头像
    private lazy var userImageView : UIImageView = {
        let view =   UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let imageUrl = LWUserAccountViewModel.sharedUserAccoutnViewModel.userAccount?.profile_image_url{
            //标示URL 不为nil  - 占位图
            view.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        view.layer.cornerRadius = view.width / 4
        view.layer.masksToBounds = true
        
        return view
    }()
    
    //文本信息
    
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        
        if let name = LWUserAccountViewModel.sharedUserAccoutnViewModel.userAccount?.name{
            label.text = "欢迎回来\(name)"
        }else{
            label.text = "欢迎回来"
        }
    
        
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        return label
    }()
    //loadView方法
    override func loadView() {
        view = bgImageView
    }
    //viewDidload
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //添加控件约束
    func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        //约束
        userImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }
    }
    
    //界面已经显示出来的时候调用动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    //开启动画
    private func startAnimation(){
     
        userImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //执行约束动画
        //3.阻尼 0 - 1 阻尼越大弹簧效果越小
        //4.初始速度
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            //动画代码
            self.view.layoutIfNeeded()
            
            }) { (_) in
                //动画完成
                
                UIView.animate(withDuration: 1, animations: { 
                    
                    self.messageLabel.alpha = 1

                    }, completion: { (_) in
                        //返回到首页
                        NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: self)
                })
        }
    }
    
    

}
